package com.example.mooctao.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.mooctao.R;
import com.example.mooctao.util.FindViewModel;
import com.example.mooctao.util.Find_Hot_Msg;
import com.example.mooctao.util.DataUtil;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//发现页面
public class FindFragment  extends Fragment {
    private GridView grids1;
    private GridView grids2;
    private List<Map<String, Object>> data1 = new ArrayList<>();
    private List<Map<String, Object>> data2 = new ArrayList<>();
    private List<Find_Hot_Msg> list = new ArrayList<>();
    private ListView listView;
    private SimpleAdapter adapter1;
    private SimpleAdapter adapter2;
    private FindViewModel findViewModel;
    //创建视图
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_find, container, false);

        // 初始化控件
        grids1 = v.findViewById(R.id.find_grids1);
        grids2 = v.findViewById(R.id.find_grids2);
        listView = v.findViewById(R.id.find_listview);
        findViewModel = new ViewModelProvider(this).get(FindViewModel.class);
        data1 = findViewModel.getData1() != null ? findViewModel.getData1() : new ArrayList<>();
        data2 = findViewModel.getData2() != null ? findViewModel.getData2() : new ArrayList<>();
        initData();
        setGridView1();
        setGridView2();
        setListView();

        return v;
    }

    // 热门头条块ListView
    private void setListView() {
        // 通过自定义适配器BaseAdapter完成
        BaseAdapter adapter = new BaseAdapter() {
            // 获取数量（设置ListView的长度）
            @Override
            public int getCount() {
                return list.size();
            }

            // 获取视图（设置ListView每一项的显示效果）
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder;
                //完成对convertView的设置
                /// 将布局资源转为view
                if (convertView == null) {
                    //优化：利用进入RecycleBin中的View，减少view的重复赋值
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.find_hot_listview, null);
                    holder = new ViewHolder();
                    holder.title = convertView.findViewById(R.id.listview_title);
                    holder.from = convertView.findViewById(R.id.listview_from);
                    holder.eye = convertView.findViewById(R.id.listview_eyenum);
                    holder.like = convertView.findViewById(R.id.listview_likenum);
                    holder.img = convertView.findViewById(R.id.listview_img);
                    holder.isLike=convertView.findViewById(R.id.listview_likeimg);
                    //listview_likeimg

                    convertView.setTag(holder);
                } else {
                    // 优化：
                    // 通过getTag()取出ViewHolder对象，然后能够直接通过holder.控件的方式在外面直接操作控件
                    // 从而避免了大幅度使用findViewById操作
                    // getTag()操作效率比findViewById要高
                    holder = (ViewHolder) convertView.getTag();
                }
                Find_Hot_Msg fhm = list.get(position);
                //标题title
                holder.title.setText(fhm.getTitle());
                //来源from
                holder.from.setText(fhm.getFrom());
                //阅读数量eye
                holder.eye.setText(fhm.getEye());
                //点赞数量like
                holder.like.setText(fhm.getLike());
                //图片img
                holder.img.setImageResource(fhm.getPic());
                //是否点赞
                if(fhm.isLike()){
                    holder.isLike.setImageResource(R.mipmap.find_goods);
                }else {
                    holder.isLike.setImageResource(R.mipmap.find_goods_normal);
                }
                return convertView;
            }

            // ==============酱油方法
            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }
        };

        listView.setAdapter(adapter);
    }

    // 初始化数据

    private void initData() {
        // 一级菜单数据
        if(data1.isEmpty()) {
            for (int i = 0; i < DataUtil.find_menu1_imgs.length; i++) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("img", DataUtil.find_menu1_imgs[i]);
                map1.put("txt", DataUtil.find_menu1_txts[i]);
                data1.add(map1);
            }
            findViewModel.setData1(data1);
        }

        // 二级菜单数据
            if(data2.isEmpty()) {
                for (int i = 0; i < DataUtil.find_menu2_imgs.length; i++) {
                    Map<String, Object> map2 = new HashMap<>();
                    map2.put("img", DataUtil.find_menu2_imgs[i]);
                    map2.put("txt1", DataUtil.find_menu2_txts1[i]);
                    map2.put("txt2", DataUtil.find_menu2_txts2[i]);
                    data2.add(map2);
                }

                findViewModel.setData2(data2);
            }

        // 热点头条数据
        for (int i = 0; i < DataUtil.find_hot_eyes.length; i++) {
            Find_Hot_Msg fhm = new Find_Hot_Msg(DataUtil.find_hot_titles[i], DataUtil.find_hot_froms[i], DataUtil.find_hot_eyes[i]
                    , DataUtil.find_hot_likes[i], DataUtil.find_hot_imgs[i],DataUtil.find_is_liked[i]);
            list.add(fhm);
        }
    }

    // 发现页一级菜单
    private void setGridView1() {
        if (adapter1 == null) {
            String[] from = {"img", "txt"};
            int[] to = {R.id.find_image_grid1, R.id.find_text_grid1};
            adapter1 = new SimpleAdapter(getContext(), data1, R.layout.find_menu_grid1, from, to);
        }
        // 设置 Adapter
        grids1.setAdapter(adapter1);
    }

    // 发现页二级菜单
    private void setGridView2() {
        if (adapter2 == null) {
            String[] from = {"img", "txt1", "txt2"};
            int[] to = {R.id.find_image_grid2, R.id.find_text1_grid2, R.id.find_text2_grid2};
            adapter2 = new SimpleAdapter(getContext(), data2, R.layout.find_menu_grid2, from, to);
        }
        // 设置 Adapter
        grids2.setAdapter(adapter2);
    }


    static class ViewHolder {
        TextView title, from, eye, like;
        ImageView img,isLike;
    }


}
